package top.lshaci.learning.springboot.mqtt.config;

import lombok.AllArgsConstructor;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import top.lshaci.learning.springboot.mqtt.properties.MqttProperties;

/**
 * MqttConfig
 *
 * @author liangxu
 */
@Configuration
@AllArgsConstructor
@EnableConfigurationProperties(MqttProperties.class)
public class MqttConfig {

    private final MqttProperties properties;

    @Bean
    public MqttConnectOptions mqttConnectOptions() {
        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
        mqttConnectOptions.setServerURIs(properties.serverUris());
        mqttConnectOptions.setUserName(properties.getUsername());
        mqttConnectOptions.setPassword(properties.getPassword().toCharArray());
        mqttConnectOptions.setConnectionTimeout(properties.getConnectionTimeout());
        mqttConnectOptions.setKeepAliveInterval(properties.getKeepAliveInterval());
        mqttConnectOptions.setCleanSession(properties.isCleanSession());
        mqttConnectOptions.setAutomaticReconnect(properties.isAutomaticReconnect());
        mqttConnectOptions.setMaxReconnectDelay(properties.getMaxReconnectDelay());
        return mqttConnectOptions;
    }

    @Bean
    public MqttPahoClientFactory mqttPahoClientFactory() {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        factory.setConnectionOptions(mqttConnectOptions());
        return factory;
    }

    @Bean
    @ServiceActivator(inputChannel = "mqttOutboundChannel")
    public MessageHandler mqttOutbound() {
        MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(properties.getClientId(), mqttPahoClientFactory());
        messageHandler.setAsync(true);
        messageHandler.setDefaultTopic(properties.getDefaultTopic());
        return messageHandler;
    }

    @Bean
    public MessageChannel mqttOutboundChannel() {
        return new DirectChannel();
    }


    @Bean
    public MessageProducer inbound() {
        MqttProperties.Consumer consumer = properties.getConsumer();
        // 可以同时消费（订阅）多个Topic
        MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(consumer.getClientId(), mqttPahoClientFactory(), consumer.defaultTopic());
        adapter.setCompletionTimeout(consumer.getCompletionTimeout());
        adapter.setDisconnectCompletionTimeout(consumer.getDisconnectCompletionTimeout());
        adapter.setRecoveryInterval(consumer.getRecoveryInterval());
        adapter.setConverter(new DefaultPahoMessageConverter());
        adapter.setQos(1);
        // 设置订阅通道
        adapter.setOutputChannel(mqttInboundChannel());
        return adapter;
    }

    @Bean(name = "mqttInboundChannel")
    public MessageChannel mqttInboundChannel() {
        return new DirectChannel();
    }

    @Bean
    @ServiceActivator(inputChannel = "mqttInboundChannel")
    public MessageHandler handler() {
        return message -> System.out.println("MqttSenderConfigClass.handleMessage " + message.getPayload() + message);
    }


}
